home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 26 / develop issue 26 code / qt conferencing code / watcher & 'caster / watcher.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-02  |  9.7 KB  |  449 lines

  1. /*
  2.      File:        Watcher.c
  3.                  Main source file for Watcher, a sample QuickTime Conferencing application
  4.                  Loosely on Philco, a SeeWorld extravaganza, courtesy Guy Riddle.
  5.  
  6.      Copyright:    © 1995 by Apple Computer, Inc.
  7.                  All rights reserved.
  8. */
  9.  
  10. /* Includes ****************************************/
  11. #define     SystemSevenOrLater 1
  12. #include <DiskInit.h>
  13. #include <Fonts.h>
  14. #include <Resources.h>
  15. #include <AppleEvents.h>
  16. #include <ToolUtils.h>
  17. #include <SegLoad.h>
  18. #include <Devices.h>
  19. #include <String.h>
  20.  
  21. #include "QuickTimeConferencing.h"
  22.  
  23. #include "watcher.h"
  24. #include "watching.h"
  25. #include "shared.h"
  26.  
  27. /* Prototypes **************************************/
  28. void         InitMac( void );
  29.  
  30. ComponentResult         InitApp( void );
  31.  
  32. void         EventLoop( void );
  33.  
  34. Boolean        ProcessUserEvent( void );
  35. Boolean        HandleMouseDown( EventRecord* currEvent );
  36. Boolean        HandleKeyDown( EventRecord* currEvent );
  37. Boolean        HandleHighLevelEvent( EventRecord* currEvent);
  38. void        HandleDiskEvent( EventRecord*    currEvent );
  39.  
  40. void        AdjustMenus( void );
  41. Boolean        HandleMenuItem( long    selection );
  42. Boolean        HandleAppleMenu( short menuItem );
  43. Boolean        HandleFileMenu( short menuItem );
  44. Boolean        HandleEditMenu( short menuItem );
  45.  
  46. ComponentResult DoWatch(void);
  47. ComponentResult BrowseName( MTNamePtr name );
  48.  
  49. /*************************************************** 
  50.  InitApp 
  51. ****************************************************/
  52. ComponentResult InitApp( void ) {
  53.  
  54.     ComponentResult    result = noErr;
  55.     Handle            menuBar;
  56.     Str63            userName;
  57.     
  58.     menuBar = GetNewMBar(kMenuBarID);
  59.     if (menuBar)
  60.         {
  61.         SetMenuBar(menuBar);
  62.         DisposHandle(menuBar);
  63.         AddResMenu(GetMHandle(kAppleMenuID), 'DRVR');
  64.         DrawMenuBar();
  65.         }
  66.     else
  67.         result = ResError();
  68.         
  69.     /* open up the standard QTC conference component */
  70.     if (result == noErr)
  71.         {
  72.         GetUserName( userName );
  73.         result = CreateWatchConference( userName );
  74.         }
  75.  
  76.     return result;    
  77. }
  78.  
  79. /*************************************************** 
  80.  Main 
  81. ****************************************************/
  82. void main( void ) {
  83.  
  84.     ComponentResult result = noErr;
  85.     
  86.     DialogPtr splash;
  87.     
  88.     InitMac();
  89.     
  90.     splash = OpenSplashScreen();
  91.     
  92.     result = InitApp();
  93.     
  94.     if (splash)
  95.         DisposDialog(splash);
  96.         
  97.     if (result == noErr)    
  98.         EventLoop(); 
  99.     else
  100.         DisplayErrorAlert( result );
  101.         
  102.     TearDownWatch();
  103.     
  104.     ExitToShell();        
  105. }
  106.  
  107. /*************************************************** 
  108.  InitMac 
  109. ****************************************************/
  110. void InitMac( void ) {
  111.     InitGraf((Ptr) &qd.thePort);
  112.     InitFonts();
  113.     InitWindows();
  114.     InitMenus();
  115.     TEInit();
  116.     InitDialogs(nil);
  117.     InitCursor();
  118.     EnterMovies();
  119. }
  120.  
  121. /*************************************************** 
  122.  EventLoop 
  123. ****************************************************/
  124. void EventLoop( void ) {
  125.  
  126.     Boolean  bail;
  127.     
  128.     do {
  129.     
  130.         DisplayErrorAlert( CheckConferenceEvents() );
  131.         
  132.         bail = ProcessUserEvent();
  133.         
  134.     } while (bail == false);
  135. }
  136.  
  137.  
  138. /*************************************************** 
  139.  ProcessUserEvent 
  140. ****************************************************/
  141. Boolean        ProcessUserEvent( void ) {
  142.     Boolean         bail = false;
  143.     EventRecord        currEvent;
  144.     
  145.     WaitNextEvent(everyEvent, &currEvent, 1, 0);
  146.  
  147.     if( !IsConferenceEvent( &currEvent) )
  148.         {
  149.         switch (currEvent.what) {
  150.             case    mouseDown:
  151.                 bail = HandleMouseDown( &currEvent );
  152.                 break;
  153.                 
  154.             case    keyDown:
  155.                 bail = HandleKeyDown( &currEvent );
  156.                 break;
  157.                 
  158.             case    kHighLevelEvent:
  159.                 bail = HandleHighLevelEvent( &currEvent);
  160.                 break;
  161.                 
  162.             case diskEvt:
  163.                 HandleDiskEvent( &currEvent );
  164.                 break;
  165.             
  166.             default:
  167.                 break;
  168.             }
  169.         }
  170.     return bail;
  171. }
  172.  
  173.  
  174. /*************************************************** 
  175.  HandleMouseDown 
  176. ****************************************************/
  177. Boolean    HandleMouseDown( EventRecord* currEvent ) {
  178.     Boolean                 bail = false;
  179.     WindowPtr                eventWindow;
  180.     short                    part;
  181.     long                    whichMenuItem;
  182.     
  183.     part = FindWindow(currEvent->where, &eventWindow);
  184.     
  185.     switch(part){
  186.         case inMenuBar:
  187.             AdjustMenus();
  188.             whichMenuItem = MenuSelect( currEvent->where );
  189.             bail = HandleMenuItem( whichMenuItem );
  190.             break;
  191.         case inDrag:
  192.             DragWindow( eventWindow, currEvent->where, &(*GetGrayRgn())->rgnBBox );
  193.             break;
  194.         case inGoAway:
  195.             if (TrackGoAway( eventWindow, currEvent->where ) )
  196.                 CloseWatch( eventWindow );
  197.             break;
  198.         default:        
  199.             break;
  200.     }
  201.  
  202.     return bail;
  203. }
  204.  
  205. /*************************************************** 
  206.  HandleKeyDown 
  207. ****************************************************/
  208. Boolean    HandleKeyDown( EventRecord* currEvent ) {
  209.     Boolean bail = false;
  210.     long    whichMenuItem;
  211.     
  212.     if(currEvent->modifiers & cmdKey){
  213.     
  214.         AdjustMenus();
  215.         
  216.         whichMenuItem = MenuKey(currEvent->message & charCodeMask);
  217.         
  218.         bail = HandleMenuItem(whichMenuItem);
  219.     }
  220.  
  221.     return bail;
  222. }
  223.  
  224. /*************************************************** 
  225.  HandleHighLevelEvent 
  226. ****************************************************/
  227. Boolean    HandleHighLevelEvent( EventRecord* currEvent) {
  228.  
  229.     Boolean bail = false;
  230.     
  231.     AEProcessAppleEvent(currEvent);
  232.     
  233.     return bail;
  234. }
  235.  
  236. /*************************************************** 
  237.  HandleDiskEvent 
  238. ****************************************************/
  239. void    HandleDiskEvent( EventRecord*    currEvent ) {
  240.     Point                    where;
  241.  
  242.     if(HiWord(currEvent->message)){
  243.         SetPt(&where, kDILeft, kDITop);
  244.         DIBadMount(where, currEvent->message);
  245.     }
  246. }
  247.  
  248. /*************************************************** 
  249.  HandleMenuItem 
  250. ****************************************************/
  251. Boolean    HandleMenuItem( long    selection ) {
  252.     Boolean bail = false;
  253.     short    menuID = HiWord(selection);
  254.     short    menuItem = LoWord(selection);
  255.     
  256.     switch (menuID) {
  257.         case kAppleMenuID:
  258.             bail = HandleAppleMenu( menuItem );
  259.             break;
  260.         case kFileMenuID:
  261.             bail = HandleFileMenu( menuItem );
  262.             break;
  263.         case kEditMenuID:
  264.             bail = HandleEditMenu( menuItem );
  265.             break;
  266.         }
  267.         
  268.     HiliteMenu(0);
  269.     
  270.     return bail;
  271. }
  272.  
  273. /*************************************************** 
  274.  HandleAppleMenu 
  275. ****************************************************/
  276. Boolean    HandleAppleMenu( short menuItem ) {
  277.     Boolean bail = false;
  278.     Str255                    daName;
  279.  
  280.     switch (menuItem) {
  281.     case kAboutMenuItem:
  282.         DoAboutBox();
  283.         break;
  284.         
  285.     default:            /* all non-About items in this menu are DAs */
  286.         GetItem(GetMHandle(kAppleMenuID), menuItem, daName);
  287.         OpenDeskAcc(daName);
  288.     }
  289.     
  290.     return bail;
  291. }
  292.  
  293. /*************************************************** 
  294.  HandleFileMenu 
  295. ****************************************************/
  296. Boolean    HandleFileMenu( short menuItem ) {
  297.     Boolean bail = false;
  298.     ComponentResult    err;
  299.     
  300.     switch (menuItem) {
  301.         case kWatchMenuItem:
  302.             err = DoWatch();
  303.             break;
  304.         case kSaveWatchMenuItem:
  305.             SaveWatch();
  306.             break;
  307.         case kCloseWatchMenuItem:
  308.             err = CloseWatch( FrontWindow() );
  309.             /* check to see if this wasn't a watch window for some reason */
  310.             if (err == paramErr)
  311.                 err = noErr;
  312.             break;
  313.         case kGetWatchInfoMenuItem:
  314.             GetWatchInfo();
  315.             break;
  316.         case kQuitMenuItem:
  317.             bail = true;
  318.             break;
  319.         default:    /* there shouldn't be any */
  320.             break;
  321.         }
  322.  
  323.     DisplayErrorAlert( err );    
  324.         
  325.     return bail;
  326. }
  327.  
  328. /*************************************************** 
  329.  HandleEditMenu 
  330. ****************************************************/
  331. Boolean    HandleEditMenu( short menuItem ) {
  332.     Boolean bail = false;
  333.     Boolean    handled;
  334.     
  335.     handled = SystemEdit(menuItem-1);
  336.     
  337.     if (!handled ) /* if this is our edit... */
  338.         {
  339.         switch (menuItem) {
  340.             case kCopyMenuItem:
  341.                 DoCopy();
  342.                 break;
  343.             default:    /* we only know how to copy now... */
  344.                 break;
  345.             }
  346.         }
  347.         
  348.     return bail;
  349. }
  350.  
  351. /*************************************************** 
  352.  AdjustMenus 
  353. ****************************************************/
  354. void AdjustMenus( void ) {
  355.     MenuHandle    appleMenu = GetMHandle(kAppleMenuID);
  356.     MenuHandle    fileMenu = GetMHandle(kFileMenuID);
  357.     MenuHandle    editMenu = GetMHandle(kEditMenuID);
  358.     
  359.     EnableItem( appleMenu, kAboutMenuItem );
  360.     EnableItem( fileMenu, kQuitMenuItem );
  361.  
  362.     if (IsWatchWindow( FrontWindow() ) )
  363.         {
  364.         EnableItem( fileMenu, kCloseWatchMenuItem );
  365.         EnableItem( fileMenu, kSaveWatchMenuItem );
  366.         EnableItem( fileMenu, kGetWatchInfoMenuItem );
  367.         EnableItem( editMenu, kCopyMenuItem );
  368.         }
  369.     else
  370.         {
  371.         DisableItem( fileMenu, kCloseWatchMenuItem );
  372.         DisableItem( fileMenu, kSaveWatchMenuItem );
  373.         DisableItem( fileMenu, kGetWatchInfoMenuItem );
  374.         DisableItem( editMenu, kCopyMenuItem );
  375.         }
  376.         
  377.     DisableItem( editMenu, kUndoMenuItem );
  378.     DisableItem( editMenu, kCutMenuItem );
  379.     DisableItem( editMenu, kPasteMenuItem );
  380.     DisableItem( editMenu, kClearMenuItem );        
  381. }
  382.  
  383. /*************************************************** 
  384.  DoWatch 
  385. ****************************************************/
  386. ComponentResult DoWatch(void) {
  387.  
  388.     ComponentResult         err = noErr;
  389.     WindowPtr                 wind = nil;
  390.     Rect                    box = { 0, 0, 160, 120 + 16 };
  391.     MTName                    name;
  392.     
  393.     err =  BrowseName( &name );    
  394.     
  395.     if ( err == noErr )
  396.         {
  397.         wind = GetNewCWindow(kWatchWindowID, nil, (GrafPtr)-1);
  398.         if (wind) {
  399.             MemberRecord* member;
  400.             /* just call the first name in the list... */
  401.             err = CallMember( &name, wind, &box, true, &member );
  402.             
  403.             /* save it away in the refcon for later use */
  404.             if (err == noErr)
  405.                 {
  406.                 SetWRefCon(wind, (long)member);
  407.                 }
  408.             }
  409.         
  410.         if (err != noErr)
  411.             DisposeWindow( wind );
  412.         }
  413.         
  414.     if (err == userCanceledErr)
  415.         err = noErr;
  416.         
  417.     return err;
  418. }
  419.  
  420. /*************************************************** 
  421.  BrowseName 
  422. ****************************************************/
  423. ComponentResult BrowseName( MTNamePtr name ) {
  424.     MTNameListPtr            allNames = 0;
  425.     ComponentResult         err;
  426.     MTBrowserComponent         browser = nil;
  427.  
  428.     browser = OpenDefaultComponent(kMTBrowserType, kMTAppleTalkSubType);
  429.     
  430.     if (browser)
  431.         {
  432.         err = MTBrowserBrowse(browser, 0, nil, (MTCString)"mtlkatlk\tMulticaster\x0D", 0, &allNames);
  433.     
  434.         CloseComponent( browser );
  435.         }
  436.     else
  437.         err = couldntGetRequiredComponent;
  438.         
  439.     if (allNames)
  440.         {
  441.         /* copy the name record */
  442.         *name = allNames->list[0];
  443.         
  444.         /* dispose of the list of names */
  445.         DisposePtr((Ptr)allNames);
  446.         }
  447.         
  448.     return err;
  449. }